from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from .models import UserMsg
from .models import Article
from .models import ArticleType
# Create your views here.
def index(request):
    '''
    主页的函数
    '''
    if request.session.has_key('name123213'):
        nav1 = request.session['name123213']
        nav2 = '/'
        nav3 = '退出'
        nav4 = '/logout'
    else:
        nav1 = '注册'
        nav2 = '/register'
        nav3 = '登录'
        nav4 = '/login'
    articletype =list(ArticleType.objects.values('articletype'))
    # 截取前20条进行显示
    article = Article.objects.values('title', 'author_name', 'abstract', 'content')[:20]
    context = {
        'articletype': articletype,
        'article': article,
        'nav1': nav1,
        'nav2': nav2,
        'nav3': nav3,
        'nav4': nav4
    }
    return render(request, 'index.html', context=context)


def reg(request):
    '''
    注册的函数
    :param request:
    :return:
    '''
    if request.method == 'POST':
        name = request.POST['name']
        passwd = request.POST['passwd']
        email = request.POST['email']
        phone = request.POST['phone']
        if UserMsg.objects.filter(name__exact=name):
            return HttpResponse('对不起该用户已经被注册了！')
        UserMsg.objects.create(name=name, passwd=passwd, email=email, phone=phone, isadmin=False)
        return HttpResponse('注册成功')
    return render(request, 'register.html')


def login(request):
    '''
    登录的函数
    :param request:
    :return:
    '''
    if request.session.has_key('name123213'):
        return HttpResponseRedirect('/')
    if request.method == 'POST':
        name = request.POST['name']
        passwd = request.POST['passwd']
        islogin = UserMsg.objects.filter(name__exact=name, passwd__exact=passwd)
        if islogin:
            request.session['name123213'] = name
            return HttpResponseRedirect('/')
        else:
            return HttpResponse('登录失败')
    return render(request, 'login.html')


def logout(request):
    '''
    退出登录的函数
    :param request:
    :return:
    '''
    del request.session['name123213']
    return HttpResponseRedirect('/')
def publish(request):
    '''
    发布文章的函数
    :param request:
    :return:
    '''
    if request.session.has_key('name123213')==False:
        return HttpResponseRedirect('/login')
    else:
        nav1 = request.session['name123213']
        nav2 = '/'
        nav3 = '退出'
        nav4 = '/logout'
    # 查询出所有文章类别的ID和名称
    typeall=ArticleType.objects.values('id','articletype')
    typeid=[]
    typename=[]
    for item in typeall:
        typeid.append(item['id'])
        typename.append(item['articletype'])
    typeidandname=zip(typeid,typename)

    # 查找用户是不是管理员
    thisuser=list(UserMsg.objects.filter(name=request.session['name123213']).values())[0]
    isadmin=thisuser['isadmin']
    if isadmin is False:
        return HttpResponse('对不起！您不是管理员不能发布文章')
    if request.method=='POST':
        thistitle=request.POST['title']
        author=request.POST['author']
        thisabstrct=request.POST['detail']
        thistype_name=request.POST['typeid']
        thiscontent=request.POST['content']
        typename=ArticleType.objects.get(articletype=thistype_name)
        Article.objects.create(title=thistitle,author_name=author,abstract=thisabstrct,content=thiscontent,type=typename)
        return HttpResponse('文章发布成功！')
    context={
        'typeidandname':typeidandname,
    }

    return render(request,'publish.html',context=context)